/*
 * This file is part of b.AMP.
 *
 * b.AMP is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * b.AMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with b.AMP.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "runnable.hpp"
#include "message_source.hpp"
#include "message_target.hpp"
#include "message_loop.hpp"
#include "service.hpp"

#include <memory>


/*
 * Implementation.
 */
struct bamp::Service::Implementation
{
    Implementation()
        : thread(),
          target(NULL)
    {
        // Nothing here
    }

    ~Implementation()
    {
        // Nothing here
    }

    MessageLoop    thread;
    MessageTarget* target;
};

    

/*
 * Public interface.
 */


bamp::Service::Service()
    : pimpl(new Implementation)
{
    // Nothing here
}


bamp::Service::~Service()
{
    // Nothing here
}


bool bamp::Service::run()
{
    return pimpl->thread.run();
}


void bamp::Service::stop()
{
    pimpl->thread.stop();
}


void bamp::Service::waitStop()
{
    pimpl->thread.waitStop();    
}


bool bamp::Service::isRunning()
{
    return pimpl->thread.isRunning();
}


bool bamp::Service::isStopping()
{
    return pimpl->thread.isStopping();
}


void bamp::Service::setTarget(MessageTarget& target)
{
    pimpl->target = &target;
}


void bamp::Service::clearTarget()
{
    pimpl->target = NULL;
}


void bamp::Service::send(const Message& message, MessageAck ack)
{
    pimpl->thread.send(message, ack);
}


bamp::MessageAck bamp::Service::send(const Message& message)
{
    return pimpl->thread.send(message);
}


bamp::MessageLoop* bamp::Service::thread()
{
    return &pimpl->thread;
}


bamp::MessageTarget* bamp::Service::target()
{
    return pimpl->target;
}
